home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / echogs.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  8.5 KB  |  341 lines

  1. /* Copyright (C) 1992, 1995, 1997, 1998, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: echogs.c,v 1.2 2000/09/19 19:00:10 lpd Exp $ */
  20. /* 'echo'-like utility */
  21. #include "stdpre.h"
  22. #include <stdio.h>
  23. /* Some brain-damaged environments (e.g. Sun) don't include */
  24. /* prototypes for fputc/fputs in stdio.h! */
  25. extern int fputc(P2(int, FILE *)), fputs(P2(const char *, FILE *));
  26.  
  27. /* Some systems have time_t in sys/types.h rather than time.h. */
  28. #include <sys/types.h>
  29. #include <ctype.h>
  30. #include <string.h>
  31. #include <time.h>        /* for ctime */
  32. #ifdef VMS
  33. #include <stdlib.h>
  34. #endif
  35.  
  36. /*
  37.  * This program exists solely to get around omissions, problems, and
  38.  * incompatibilities in various shells and utility environments.
  39.  * Don't count on it staying the same from one release to another!
  40.  */
  41.  
  42. /*
  43.  * Usage:
  44.  echogs [-e .extn] [-(w|a)[b][-] file] [-h] [-n]
  45.  (-b|-B | -d|-D | -f|-F | -x hexstring | -(l|q|Q) string | -(l|q|Q)string |
  46.  -s | -u string | -i | -r file | -R file | -X)*
  47.  [-] string*
  48.  * Echoes string(s), or the binary equivalent of hexstring(s).
  49.  * If -w, writes to file; if -a, appends to file; if neither,
  50.  *   writes to stdout.  -wb and -ab open the file in binary mode.
  51.  *   -w and -a search forward for the next argument that is not a switch.
  52.  *   An appended - means the same as - alone, taking effect after the file
  53.  *   argument.
  54.  * -e specifies an extension to be added to the file name.
  55.  * If -h, write the output in hex instead of literally.
  56.  * If -n, does not append a newline to the output.
  57.  * -b or -B means insert the base part (minus directories) of the file name
  58.  *   passed as the argument of -w or -a.
  59.  * -d or -D means insert the date and time.
  60.  * -f or -F means insert the file name passed as the argument of -w or -a.
  61.  * -q means write the next string literally.
  62.  * -l or -Q means the same as -q followed by -s.
  63.  * -s means write a space.
  64.  * -u means convert the next string to upper case.
  65.  * -i means read from stdin, treating each line as an argument.
  66.  * -r means read from a named file in the same way.
  67.  * -R means copy a named file with no interpretation
  68.  *   (but convert to hex if -h is in effect).
  69.  * -X means treat any following literals as hex rather than string data.
  70.  * - or -+ alone means treat the rest of the line as literal data,
  71.  *   even if the first string begins with a -.
  72.  * -+<letter> is equivalent to -<Letter>, i.e., it upper-cases the letter.
  73.  * Inserts spaces automatically between the trailing strings,
  74.  * but nowhere else; in particular,
  75.  echogs -q a b
  76.  * writes 'ab', in contrast to
  77.  echogs -q a -s b
  78.  * which writes 'a b'.
  79.  */
  80.  
  81. static int hputc(P2(int, FILE *)), hputs(P2(const char *, FILE *));
  82.  
  83. int
  84. main(int argc, char *argv[])
  85. {
  86.     FILE *out = stdout;
  87.     /*
  88.      * The initialization in = 0 is unnecessary: in is only referenced if
  89.      * interact = 1, in which case in has always been initialized.
  90.      * We initialize in = 0 solely to pacify stupid compilers.
  91.      */
  92.     FILE *in = 0;
  93.     const char *extn = "";
  94.     char fmode[4];
  95. #define FNSIZE 100
  96.     char *fnparam;
  97.     char fname[FNSIZE];
  98.     int newline = 1;
  99.     int interact = 0;
  100.     int (*eputc)(P2(int, FILE *)) = fputc;
  101.     int (*eputs)(P2(const char *, FILE *)) = fputs;
  102. #define LINESIZE 1000
  103.     char line[LINESIZE];
  104.     char sw = 0, sp = 0, hexx = 0;
  105.     char **argp = argv + 1;
  106.     int nargs = argc - 1;
  107.  
  108.     if (nargs > 0 && !strcmp(*argp, "-e")) {
  109.     if (nargs < 2)
  110.         return 1;
  111.     extn = argp[1];
  112.     argp += 2, nargs -= 2;
  113.     }
  114.     if (nargs > 0 && (*argp)[0] == '-' &&
  115.     ((*argp)[1] == 'w' || (*argp)[1] == 'a')
  116.     ) {
  117.     size_t len = strlen(*argp);
  118.     int i;
  119.  
  120.     if (len > 4)
  121.         return 1;
  122.     for (i = 1; i < nargs; i++)
  123.         if (argp[i][0] != '-')
  124.         break;
  125.     if (i == nargs)
  126.         return 1;
  127.     fnparam = argp[i];
  128.     strcpy(fmode, *argp + 1);
  129.     strcpy(fname, fnparam);
  130.     strcat(fname, extn);
  131.     if (fmode[len - 2] == '-') {
  132.         /*
  133.          * The referents of argp are actually const, but they can't be
  134.          * declared that way, so we have to make a writable constant.
  135.          */
  136.         static char dash[2] = { '-', 0 };
  137.  
  138.         fmode[len - 2] = 0;
  139.         argp[i] = dash;
  140.         argp++, nargs--;
  141.     } else {
  142.         for (; i > 1; i--)
  143.         argp[i] = argp[i - 1];
  144.         argp += 2, nargs -= 2;
  145.     }
  146.     } else
  147.     strcpy(fname, "");
  148.     if (nargs > 0 && !strcmp(*argp, "-h")) {
  149.     eputc = hputc, eputs = hputs;
  150.     argp++, nargs--;
  151.     }
  152.     if (nargs > 0 && !strcmp(*argp, "-n")) {
  153.     newline = 0;
  154.     argp++, nargs--;
  155.     }
  156.     if (strlen(fname) != 0) {
  157.     out = fopen(fname, fmode);
  158.     if (out == 0)
  159.         return 1;
  160.     }
  161.     while (1) {
  162.     char *arg;
  163.  
  164.     if (interact) {
  165.         if (fgets(line, LINESIZE, in) == NULL) {
  166.         interact = 0;
  167.         if (in != stdin)
  168.             fclose(in);
  169.         continue;
  170.         }
  171.         /* Remove the terminating \n. */
  172.         line[strlen(line) - 1] = 0;
  173.         arg = line;
  174.     } else {
  175.         if (nargs == 0)
  176.         break;
  177.         arg = *argp;
  178.         argp++, nargs--;
  179.     }
  180.     if (sw == 0 && arg[0] == '-') {
  181.         char chr = arg[1];
  182.  
  183.         sp = 0;
  184.       swc:switch (chr) {
  185.         case 'l':    /* literal string, then -s */
  186.             chr = 'Q';
  187.             /* falls through */
  188.         case 'q':    /* literal string */
  189.         case 'Q':    /* literal string, then -s */
  190.             if (arg[2] != 0) {
  191.             (*eputs) (arg + 2, out);
  192.             if (chr == 'Q')
  193.                 (*eputc) (' ', out);
  194.             break;
  195.             }
  196.             /* falls through */
  197.         case 'r':    /* read from a file */
  198.         case 'R':
  199.         case 'u':    /* upper-case string */
  200.         case 'x':    /* hex string */
  201.             sw = chr;
  202.             break;
  203.         case 's':    /* write a space */
  204.             (*eputc) (' ', out);
  205.             break;
  206.         case 'i':    /* read interactively */
  207.             interact = 1;
  208.             in = stdin;
  209.             break;
  210.         case 'b':    /* insert base file name */
  211.         case 'B':
  212.             arg = fnparam + strlen(fnparam);
  213.             while (arg > fnparam &&
  214.                (isalnum(arg[-1]) || arg[-1] == '_'))
  215.             --arg;
  216.             (*eputs) (arg, out);
  217.             break;
  218.         case 'd':    /* insert date/time */
  219.         case 'D':
  220.             {
  221.             time_t t;
  222.             char str[26];
  223.  
  224.             time(&t);
  225.             strcpy(str, ctime(&t));
  226.             str[24] = 0;    /* remove \n */
  227.             (*eputs) (str, out);
  228.             } break;
  229.         case 'f':    /* insert file name */
  230.         case 'F':
  231.             (*eputs) (fnparam, out);
  232.             break;
  233.         case 'X':    /* treat literals as hex */
  234.             hexx = 1;
  235.             break;
  236.         case '+':    /* upper-case command */
  237.             if (arg[1]) {
  238.             ++arg;
  239.             chr = toupper(arg[1]);
  240.             goto swc;
  241.             }
  242.             /* falls through */
  243.         case 0:        /* just '-' */
  244.             sw = '-';
  245.             break;
  246.         }
  247.     } else
  248.         switch (sw) {
  249.         case 0:
  250.         case '-':
  251.             if (hexx)
  252.             goto xx;
  253.             if (sp)
  254.             (*eputc) (' ', out);
  255.             (*eputs) (arg, out);
  256.             sp = 1;
  257.             break;
  258.         case 'q':
  259.             sw = 0;
  260.             (*eputs) (arg, out);
  261.             break;
  262.         case 'Q':
  263.             sw = 0;
  264.             (*eputs) (arg, out);
  265.             (*eputc) (' ', out);
  266.             break;
  267.         case 'r':
  268.             sw = 0;
  269.             in = fopen(arg, "r");
  270.             if (in == NULL)
  271.             exit(exit_FAILED);
  272.             interact = 1;
  273.             break;
  274.         case 'R':
  275.             sw = 0;
  276.             in = fopen(arg, "r");
  277.             if (in == NULL)
  278.             exit(exit_FAILED);
  279.             while (fread(line, 1, 1, in) > 0)
  280.             (*eputc) (line[0], out);
  281.             fclose(in);
  282.             break;
  283.         case 'u':
  284.             {
  285.             char *up;
  286.  
  287.             for (up = arg; *up; up++)
  288.                 (*eputc) (toupper(*up), out);
  289.             }
  290.             sw = 0;
  291.             break;
  292.         case 'x':
  293.           xx:{
  294.             char *xp;
  295.             unsigned int xchr = 1;
  296.  
  297.             for (xp = arg; *xp; xp++) {
  298.                 char ch = *xp;
  299.  
  300.                 if (!isxdigit(ch))
  301.                 return 1;
  302.                 xchr <<= 4;
  303.                 xchr += (isdigit(ch) ? ch - '0' :
  304.                      (isupper(ch) ? tolower(ch) : ch)
  305.                      - 'a' + 10);
  306.                 if (xchr >= 0x100) {
  307.                 (*eputc) (xchr & 0xff, out);
  308.                 xchr = 1;
  309.                 }
  310.             }
  311.             }
  312.             sw = 0;
  313.             break;
  314.         }
  315.     }
  316.     if (newline)
  317.     (*eputc) ('\n', out);
  318.     if (out != stdout)
  319.     fclose(out);
  320.     return exit_OK;
  321. }
  322.  
  323. static int
  324. hputc(int ch, FILE * out)
  325. {
  326.     static const char *hex = "0123456789abcdef";
  327.  
  328.     /* In environments where char is signed, ch may be negative (!). */
  329.     putc(hex[(ch >> 4) & 0xf], out);
  330.     putc(hex[ch & 0xf], out);
  331.     return 0;
  332. }
  333.  
  334. static int
  335. hputs(const char *str, FILE * out)
  336. {
  337.     while (*str)
  338.     hputc(*str++ & 0xff, out);
  339.     return 0;
  340. }
  341.